import express from 'express';
import Ret, { RetCode } from '../../../bean/code/Ret';
const app = express();
import { Op } from 'Sequelize'
import { db, Sequelize } from '../../../dao/config/index';
const User = require("../../../dao/models/f_user")(db, Sequelize);
const UserRealInfo = require("../../../dao/models/f_user_real_info")(db, Sequelize);
import _ from 'lodash'
import { randomCode } from '../../../utils/index';
import { sendMsg } from '../../../service/phoneMessage';
import logger from '../../../utils/logger';


/**
 * 验证用户手机号
 */
app.post("/validUserPhone", async (req, res) => {
  const { userId, phone } = req.body;
  const verificationCode = randomCode(10);

  try {
    // 暂时被封
    // const msgRes = await sendMsg({ PhoneNumbers: phone, code: verificationCode });
    const userInfoModel = await UserRealInfo.findOrCreate({ where: { userId }, default: { userId, verificationCode } }).spread(i => i);
    const updateRes = await userInfoModel.update({ verificationCode });
  } catch (error) {
    logger.error(error);
    return res.json(Ret(RetCode.ERROR).setMsg(error));
  }
  return res.json(Ret(RetCode.SUCCESS).setData(verificationCode));

})
/**
 * 更新已验证的用户真实信息
 */
app.post("/insertUserRealInfo", async (req, res) => {
  const realInfo = req.body, { userId } = realInfo;
  try {
    const updateRes = await UserRealInfo.update(realInfo,{ where: { userId }});
  } catch (error) {
    logger.error(error);
    return res.json(Ret(RetCode.ERROR).setMsg(error));
  }
  return res.json(Ret(RetCode.SUCCESS));

})

export default app;
